<?php
class ModuleController extends MController {
    public function indexAction(){
        $modules = Module::all();
        $moduleTable = new MHtmlTable($modules);
        $buttons = array('create'=>array('title'=>'Créer', 'href'=>__route('ml-admin/module/create'), 'content'=>'Créer'), 'update'=>array('title'=>'Mise à jour', 'href'=>__route('ml-admin/module/update', array('id'=>'{id}')), 'content'=>'Update'), 'delete'=>array('title'=>'Supprimer', 'href'=>  __route('ml-admin/module/delete', array('id'=>'{id}')), 'content'=>'Supprimer'));
        $moduleTable->addColumn(new MHtmlButtonColumn($moduleTable,$buttons));
        $formInstall = new ModuleInstallForm();
        
        $this->render('index', array('modules'=>$modules, 'formInstall'=>$formInstall, 'moduleTable'=>$moduleTable));
    }
    
    public function installAction(){
        if ($this->getRequest()->isPost()){
            if (isset($_FILES['pathFile']['name']) && !empty($_FILES['pathFile']['name'])){
                $moduleName = basename($_FILES['pathFile']['name'], '.zip');
                if (!Module::exists(array('name'=>$moduleName))){
                    MZip::extractTo($_FILES['pathFile']['tmp_name'], BASE_PATH.DS.'modules');
                    $extractedDir = BASE_PATH.DS.'modules'.DS.$moduleName;
                    if (file_exists($extractedDir)){
                        $sql = MFile::getSQLQueries($extractedDir.DS.'install'.DS.'tables.sql');
                        foreach ($sql as $query){
                            if (isset($query) && !empty($query)){
                                ActiveRecord\Connection::instance()->query($query.';');
                            }
                        }
                        require_once $extractedDir.DS.$moduleName.'.php';
                        $moduleClass = MNormalizer::normalizeModuleClass($moduleName);
                        Module::create(array('name'=>$moduleName, 'version'=>$moduleClass::getVersion(), 'active'=>true));
                    }
                } else {
                    $message = __t('mandrill.admin.modules.alreadyInstalled');
                }
            } else {
                $message = __t('mandrill.admin.modules.specifyPath');
            }
            $this->render('install', array('message'=>$message));
        } else {
            MRequest::redirect(__route('ml-admin'));
        }
    }
    
    public function deleteAction(){
        $get = $this->getRequest()->getGet();
        if (isset($get['id']) && !empty($get['id'])){
            $module = Module::find($get['id']);
            if ($module !== NULL){
                $module->delete();
            }
            $queries = MFile::getSQLQueries(BASE_PATH.DS.'modules'.DS.$module->name.DS.'install'.DS.'drop.sql');
            foreach ($queries as $query){
                if (isset($query) && !empty($query)){
                    ActiveRecord\Connection::instance()->query($query);
                }
            }
            MFile::removeDirectoryRecursively(BASE_PATH.DS.'modules'.DS.$module->name);
        }
        MRequest::redirect(__route('ml-admin/module'));
    }
}
?>
